package com.dl.mall.controller.user;

import com.dl.mall.annotation.MethodInfo;
import com.dl.mall.config.satoken.StpUserUtil;
import com.dl.mall.entity.user.Address;
import com.dl.mall.service.user.AddressService;
import com.dl.mall.utils.UUIDUtils;
import com.dl.mall.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 地址类controller
 * @author DL
 * @create 2022-03-21 20:19
 */
@Slf4j
@RestController()
@RequestMapping("/address")
@Api(tags="地址类controller")
public class AddressController {

    @Resource
    AddressService addressService;

    /**
     * 增加地址
     * @param address 地址
     * @return 结果
     */
    @ApiOperation(value="增加地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name="address",value="地址",required=true,paramType="body"),
    })
    @MethodInfo
    @PostMapping("/add")
    public Result addAddress(@RequestBody Address address) {
        System.out.println("address = " + address);
        address.setUserId(StpUserUtil.getLoginIdAsString());
        address.setAddressId(UUIDUtils.getUUID10());
        int result = addressService.addAddress(address);

        if (result != 0){
            return Result.ok("增加地址成功");
        }else {
            return Result.error("增加地址失败");
        }
    }

    @ApiOperation(value="修改默认地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name="addressId",value="地址id",required=true,paramType="path"),
    })
    @MethodInfo
    @GetMapping(value = {"/setDefault/{addressId}"})
    public Result updateAddress(@PathVariable String addressId) {
        System.out.println("addressId = " + addressId);

        int result = addressService.setDefaultAddress(StpUserUtil.getLoginIdAsString(), addressId);

        if (result != 0){
            return Result.ok("修改默认地址成功");
        }else {
            return Result.error("修改默认地址失败");
        }
    }

    @ApiOperation(value="修改地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name="address",value="地址",required=true,paramType="body"),
    })
    @MethodInfo
    @PostMapping("/update")
    public Result updateAddress(@RequestBody Address address) {
        System.out.println("address = " + address);

        int result = addressService.modifyAddress(address);

        if (result != 0){
            return Result.ok("修改地址成功");
        }else {
            return Result.error("修改地址失败");
        }
    }

    /**
     * 查询地址总数
     * @return 结果
     */
    @GetMapping("/count")
    public Result getTotalAddress() {
        int totalAddress = addressService.getTotalAddress(StpUserUtil.getLoginIdAsString());

        if (totalAddress != 0){
            return Result.ok("查询地址总数成功");
        }else {
            return Result.error("查询地址总数错误");
        }
    }

    @ApiOperation(value="根据 地址id 查询地址信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="addressId",value="地址id",required=true,paramType="path"),
    })
    @MethodInfo
    @GetMapping(value = {"/{addressId}"})
    public Result getAddressInfo(@PathVariable String addressId){
        System.out.println("addressId = " + addressId);

        Address address = addressService.getAddressById(addressId);
        System.out.println("address = " + address);
        if (address != null){
            return Result.ok("查询地址成功").data("address", address);
        }else {
            return Result.error("查询地址失败");
        }
    }


    @ApiOperation(value="根据 用户id 查询默认地址信息")
    @MethodInfo
    @GetMapping("/default")
    public Result getDefaultAddress() {

        Address defaultAddress = addressService.getDefaultAddress(StpUserUtil.getLoginIdAsString());

        if (defaultAddress != null){
            return Result.ok("查询默认地址成功").data("address",defaultAddress);
        }else {
            return Result.error("查询默认地址错误");
        }
    }

    @ApiOperation(value="查询用户所有地址")
    @MethodInfo
    @GetMapping("/all")
    public Result getAllAddress() {
        System.out.println("StpUserUtil.getLoginIdAsString() = " + StpUserUtil.getLoginIdAsString());
        List<Address> allAddress = addressService.getAllAddress(StpUserUtil.getLoginIdAsString());
        if (allAddress != null){
            return Result.ok("查询所有地址成功").data("allAddress",allAddress);
        }else {
            return Result.error("查询所有地址错误");
        }
    }

}
